package com.dk.service.impl;

import com.dk.mapper.PositionMapper;
import com.dk.mapper.PositionMenuMapper;
import com.dk.pojo.Position;
import com.dk.pojo.PositionMenuExample;
import com.dk.service.PosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PosServiceImpl implements PosService {
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private PositionMenuMapper positionMenuMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Position> getInfo() {
        return positionMapper.selectByExample(null);
    }

    @Override
    public boolean addInfo(Position position, Integer[] nodeIds) {
        positionMapper.insertById(position);
        positionMenuMapper.insertById(position.getPositionId(),nodeIds);
        return true;
    }

    @Override
    public Position toUpdate(Integer positionId) {
        return positionMapper.selectByPrimaryKey(positionId);
    }

    @Override
    public boolean updateInfo(Position position, Integer[] nodeIds) {
        positionMapper.updateByPrimaryKeySelective(position);

        PositionMenuExample pme = new PositionMenuExample();
        PositionMenuExample.Criteria criteria = pme.createCriteria();
        criteria.andPositionIdEqualTo(position.getPositionId());
        positionMenuMapper.deleteByExample(pme);

        positionMenuMapper.insertById(position.getPositionId(),nodeIds);

        //删除redis中的菜单缓存数据
        redisTemplate.delete("permission");
        return true;
    }

    @Override
    public boolean deleteInfo(Integer id) {
        positionMapper.deleteByPrimaryKey(id);

        PositionMenuExample pme = new PositionMenuExample();
        PositionMenuExample.Criteria criteria = pme.createCriteria();
        criteria.andPositionIdEqualTo(id);
        positionMenuMapper.deleteByExample(pme);

        //删除redis中的菜单缓存数据
        redisTemplate.delete("permission");

        return true;
    }
}
